/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.eventservice;

import im.general.AbstractListenBehaviour;
import im.general.Config;
import im.messages.PublishMessage;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author bluejack
 */
public class PublishListenBehaviour extends AbstractListenBehaviour {
    EventServiceAgent eventServiceAgent;
    
    public PublishListenBehaviour(EventServiceAgent eventServiceAgent) {
        this.eventServiceAgent = eventServiceAgent;
        mt = MessageTemplate.MatchEncoding(Config.PUBLISH_NAME);
        //mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
        //mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
        
        //this.mt = MessageTemplate.MatchPerformative(ACLMessage.REQUEST_WHENEVER);

        /*ACLMessage wrap_msg = new ACLMessage( ACLMessage.INFORM );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.PUBLISH_NAME);
        this.mt = MessageTemplate.MatchCustom(wrap_msg, true);*/

    }
    
    @Override
    protected void handleMessage(ACLMessage msg) {
        //System.out.println("[PublishListenBehaviour]");

        PublishMessage publishMessage = null;
        try {

            publishMessage = (PublishMessage) msg.getContentObject();
        } catch (UnreadableException ex) {
            Logger.getLogger(PublishListenBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }
     
        //System.out.println("["+this.getClass().getName()+"]"+"event received "+publishMessage.getEvent().getId());
        eventServiceAgent.publish(publishMessage);
    }

}
